home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / access.c next >
C/C++ Source or Header  |  1997-09-09  |  1KB  |  68 lines

  1.  
  2. /*
  3.  *  ACCESS.C        Check File Accessibility
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <libraries/dos.h>
  12. #include <clib/dos_protos.h>
  13. #include <stdlib.h>
  14.  
  15. #ifndef UnixToAmigaPath
  16. #define UnixToAmigaPath(path)   path
  17. #endif
  18.  
  19. typedef struct FileInfoBlock    FIB;
  20.  
  21. int
  22. access(name, mode)
  23. const char *name;
  24. int mode;
  25. {
  26.     FIB *fib = malloc(sizeof(FIB));
  27.     int r = -1;
  28.  
  29.     if (fib) {
  30.     BPTR lock;
  31.     if (lock = Lock(UnixToAmigaPath(name), SHARED_LOCK)) {
  32.         if (Examine(lock, fib)) {
  33.         long prot = fib->fib_Protection;
  34.  
  35.         r = 0;
  36.         if (mode & 4) {     /*  r   */
  37.             if (prot & 8)   /*  no read perm    */
  38.             r = -1;
  39.         }
  40.         if (mode & 2) {     /*  w   */
  41.             if (prot & 4)   /*  no write perm   */
  42.             r = -1;
  43.         }
  44.         if (mode & 1) {     /*  x   */
  45.             if (prot & 2)   /*  no execute perm */
  46.             r = -1;
  47.         }
  48.         }
  49.         UnLock(lock);
  50.     } else {
  51.         /*
  52.          *    if obj in use we cannot obtain stats.  All we know is
  53.          *    that the obj exists.  We certainly cannot read or write
  54.          *    it at the moment.
  55.          */
  56.  
  57.         if (IoErr() == ERROR_OBJECT_IN_USE) {
  58.         r = 0;
  59.         if (mode)       /*  anything but existance fails    */
  60.             r = -1;
  61.         }
  62.     }
  63.     free(fib);
  64.     }
  65.     return(r);
  66. }
  67.  
  68.